home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / texinfo / elisp / texnfo-tex.el < prev    next >
Lisp/Scheme  |  1992-01-15  |  14KB  |  347 lines

  1. ;;;; texnfo-tex.el
  2.  
  3. ;;; Texinfo mode TeX and hardcopy printing commands.
  4.  
  5. ;; These commands are for running TeX on a region of a Texinfo file in
  6. ;; GNU Emacs, or on the whole buffer, and for printing the resulting
  7. ;; DVI file.
  8.  
  9. ;;; Version 2.07    22 October 1991
  10. ;;; Robert J. Chassell      
  11. ;;; Please send bug reports to:  bob@gnu.ai.mit.edu
  12.  
  13. ;;; Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
  14.  
  15.  
  16. ;;; This file is part of GNU Emacs.
  17.  
  18. ;; GNU Emacs is free software; you can redistribute it and/or modify
  19. ;; it under the terms of the GNU General Public License as published by
  20. ;; the Free Software Foundation; either version 1, or (at your option)
  21. ;; any later version.
  22.  
  23. ;; GNU Emacs is distributed in the hope that it will be useful,
  24. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. ;; GNU General Public License for more details.
  27.  
  28. ;; You should have received a copy of the GNU General Public License
  29. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  30. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  31.  
  32.  
  33. ;;; The Texinfo mode TeX related commands are:
  34.  
  35. ; texinfo-tex-region        to run tex on the current region.
  36. ; texinfo-tex-buffer        to run tex on the current buffer.
  37. ; texinfo-texindex          to sort unsorted index files.
  38. ; texinfo-tex-print         to print the .dvi file made by tex.
  39. ; texinfo-kill-tex-job      to kill the currently running tex job.
  40. ; texinfo-recenter-tex-output-buffer    to redisplay tex output buffer.
  41. ; texinfo-show-tex-print-queue          to show the print queue.
  42.  
  43.  
  44. ;;; Keys common both to Texinfo mode and to TeX shell.
  45.  
  46. ;; Defined in `texinfo.el'
  47. ; (defun texinfo-define-common-keys (keymap)
  48. ;   "Define the keys both in Texinfo mode and in the texinfo-tex-shell."
  49. ;   (define-key keymap "\C-c\C-t\C-k"    'texinfo-kill-tex-job)
  50. ;   (define-key keymap "\C-c\C-t\C-x"    'texinfo-quit-tex-job)
  51. ;   (define-key keymap "\C-c\C-t\C-l"    'texinfo-recenter-tex-output-buffer)
  52. ;   (define-key keymap "\C-c\C-t\C-d"    'texinfo-delete-from-tex-print-queue)
  53. ;   (define-key keymap "\C-c\C-t\C-q"    'texinfo-show-tex-print-queue)
  54. ;   (define-key keymap "\C-c\C-t\C-p"    'texinfo-tex-print)
  55. ;   (define-key keymap "\C-c\C-t\C-i"    'texinfo-texindex)
  56. ;   (define-key keymap "\C-c\C-t\C-r"    'texinfo-tex-region)
  57. ;   (define-key keymap "\C-c\C-t\C-b"    'texinfo-tex-buffer))
  58.  
  59. ;; See also texinfo-tex-start-shell. 
  60. ;; The following is executed in the `texinfo.el' file
  61. ;(texinfo-define-common-keys texinfo-mode-map)
  62.  
  63.  
  64. ;;; Variable definitions:
  65.  
  66. (require 'shell)
  67.  
  68. (defvar texinfo-tex-shell-cd-command "cd"
  69.   "Command to give to shell running TeX to change directory.")
  70.  
  71. (defvar texinfo-tex-command "tex"
  72.   "*Command used by  texinfo-tex-region  to run tex on a region.")
  73.  
  74. (defvar texinfo-texindex-command "texindex"
  75.   "*Command used by  texinfo-texindex  to sort unsorted index files.")
  76.  
  77. (defvar texinfo-tex-dvi-print-command "lpr -d"
  78.   "*Command string used by \\[tex-print] to print a .dvi file.")
  79.  
  80. (defvar texinfo-show-tex-queue-command "lpq"
  81.   "*Command string used to show the Texinfo TeX print queue.
  82. Command is used by \\[texinfo-show-tex-print-queue] and it
  83. should show the queue that \\[texinfo-tex-print] puts jobs on.")
  84.  
  85. (defvar texinfo-delete-from-print-queue-command "lprm"
  86.   "*Command string used to delete a job from the line printer queue.
  87. Command is used by \\[texinfo-delete-from-tex-print-queue] based on
  88. number provided by a previous \\[texinfo-show-tex-print-queue]
  89. command.")
  90.  
  91. (defvar texinfo-tex-trailer "@bye"
  92.   "String appended after a region sent to TeX by texinfo-tex-region.")
  93.  
  94. (defvar texinfo-tex-original-file ""
  95.   "Original name of file on which to run TeX.")
  96.  
  97. (defvar texinfo-tex-temp-file nil
  98.   "Temporary file name used for text being sent as input to TeX.")
  99.  
  100. (defvar texinfo-tex-root-temp-file nil
  101.   "Temporary file name used for text being sent as input to TeX.")
  102.  
  103.  
  104. ;;; Texinfo TeX main functions
  105.  
  106. (defun texinfo-tex-region (beginning end)
  107.   "Run tex on the current region. 
  108.  
  109. A temporary file is written in the default directory, and tex is run
  110. in that directory.  The first line of the file is copied to the
  111. temporary file; and if the buffer has a header, it is written to the
  112. temporary file before the region itself.  The buffer's header is all
  113. lines between the strings defined by texinfo-start-of-header and
  114. texinfo-end-of-header inclusive.  The header must start in the first 100
  115. lines.  The value of texinfo-tex-trailer is appended to the temporary file
  116. after the region."
  117.   
  118.   (interactive "r")
  119.   (if (get-buffer "*texinfo-tex-shell*")
  120.       (quit-process (get-process "texinfo-tex-shell") t)
  121.     (texinfo-tex-start-shell))
  122.   
  123.   (setq texinfo-tex-root-temp-file
  124.         (expand-file-name 
  125.          (make-temp-name
  126.           (prin1-to-string (read (buffer-name))))))
  127.   
  128.   (let ((texinfo-tex-temp-file (concat texinfo-tex-root-temp-file ".tex")))
  129.     (save-excursion
  130.       (save-restriction
  131.         (widen)
  132.         (goto-char (point-min))
  133.         (forward-line 100)
  134.         (let ((search-end (point))
  135.               (header-beginning (point-min)) (header-end (point-min)))
  136.           (goto-char (point-min))
  137.           ;; Copy first line, the `\input texinfo' line, to temp file
  138.           (write-region (point) 
  139.                         (save-excursion (forward-line 1) (point))
  140.                         texinfo-tex-temp-file nil nil)
  141.           ;; Don't copy first line twice if region includes it.
  142.           (forward-line 1)
  143.           (if (< beginning  (point)) (setq beginning  (point)))
  144.           ;; Initialize the temp file with either the header or nothing
  145.           (if (search-forward texinfo-start-of-header search-end t)
  146.               (progn
  147.                 (beginning-of-line)
  148.                 (setq header-beginning (point)) ; Mark beginning of header.
  149.                 (if (search-forward texinfo-end-of-header nil t)
  150.                     (progn (beginning-of-line)
  151.                            (setq header-end (point)))   ; Mark end of header.
  152.                   (setq header-beginning (point-min))))) ; Else no header.
  153.           ;; Copy  header  to temp file.
  154.           (write-region
  155.            (min header-beginning beginning )
  156.            header-end
  157.            texinfo-tex-temp-file t nil)
  158.           ;; Copy  region  to temp file.
  159.           (write-region
  160.            (max beginning  header-end)
  161.            end
  162.            texinfo-tex-temp-file t nil)
  163.           ;; This is a kludge to insert the texinfo-tex-trailer into the
  164.           ;; texinfo-tex-temp-file.  We have to create a special buffer
  165.           ;; in which to insert the texinfo-tex-trailer first because there is
  166.           ;; no function with which to append a literal string directly
  167.           ;; to a file.
  168.           (let ((local-tex-trailer texinfo-tex-trailer)
  169.                 (temp-buffer (get-buffer-create " texinfo-trailer-buffer")))
  170.             (set-buffer temp-buffer)
  171.             (erase-buffer)
  172.             ;; make sure trailer isn't hidden by a comment
  173.             (insert-string "\n")
  174.             (if local-tex-trailer (insert local-tex-trailer))
  175.             (write-region (point-min) (point-max) 
  176.                           texinfo-tex-temp-file t nil)))
  177.         (set-process-sentinel (get-process "texinfo-tex-shell") 
  178.                               'texinfo-tex-shell-sentinel)
  179.         (send-string "texinfo-tex-shell"
  180.                      (concat texinfo-tex-shell-cd-command " "
  181.                              default-directory "\n"))
  182.         (send-string "texinfo-tex-shell"
  183.                      (concat texinfo-tex-command " "
  184.                              texinfo-tex-temp-file "\n  "))
  185.         (texinfo-recenter-tex-output-buffer 0)))))
  186.  
  187. (defun texinfo-tex-buffer (buffer)
  188.   "Run TeX on current buffer.
  189. After running TeX the first time, you may have to run \\[texinfo-texindex]
  190. and then \\[texinfo-tex-buffer] again."
  191.   (interactive 
  192.    (list
  193.     ;; Sometimes you put point into *texinfo-tex-shell*; this prompts
  194.     ;; you for the correct file regardless.
  195.     (if (and 
  196.          (string= (buffer-name (current-buffer)) "*texinfo-tex-shell*")
  197.          texinfo-tex-root-temp-file)
  198.         (read-string (format "Run TeX on: ")
  199.                      texinfo-tex-original-file)
  200.       (read-string (format "Run TeX on: ") (buffer-name (current-buffer))))))
  201.   
  202.   ;; Set to original buffer if in *texinfo-tex-shell*; otherwise,
  203.   ;; record name of current buffer.
  204.   (if (string= (buffer-name (current-buffer)) "*texinfo-tex-shell*")
  205.       (set-buffer buffer)
  206.     (setq texinfo-tex-original-file
  207.            (buffer-name (current-buffer))))
  208.  
  209.   (if (get-buffer "*texinfo-tex-shell*")
  210.       (quit-process (get-process "texinfo-tex-shell") t)
  211.     (texinfo-tex-start-shell))
  212.   (cond ((null buffer-file-name)
  213.          (error "Buffer not visiting any file!"))
  214.         ((buffer-modified-p)
  215.          (error "Buffer has been modified since last saved!"))
  216.         (t (set-process-sentinel (get-process "texinfo-tex-shell") 
  217.                                  'texinfo-tex-shell-sentinel)
  218.            (send-string "texinfo-tex-shell"
  219.                         (concat texinfo-tex-shell-cd-command 
  220.                                 " "
  221.                                 (file-name-directory
  222.                                  (buffer-file-name
  223.                                   (get-buffer buffer)))
  224.                                 "\n"))
  225.            (send-string "texinfo-tex-shell"
  226.                         (concat texinfo-tex-command " " buffer "\n  "))
  227.            
  228.            ;; so the texinfo-tex-print command works
  229.            (setq texinfo-tex-root-temp-file
  230.                  (substring buffer 0
  231.                             (or (string-match "\\.tex" buffer)
  232.                                 (length buffer))))
  233.            
  234.            (texinfo-recenter-tex-output-buffer 0))))
  235.  
  236. (defun texinfo-texindex ()
  237.   "Run texindex on unsorted index files.
  238. The index files are made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
  239. Runs the shell command defined by texinfo-texindex-command."
  240.   (interactive)
  241.   (send-string "texinfo-tex-shell"
  242.                (concat texinfo-texindex-command
  243.                        " " texinfo-tex-root-temp-file ".??" "\n"))
  244.   (texinfo-recenter-tex-output-buffer nil))
  245.  
  246. (defun texinfo-tex-print ()
  247.   "Print .dvi file made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
  248. Runs the shell command defined by texinfo-tex-dvi-print-command."
  249.   (interactive)
  250.   (send-string "texinfo-tex-shell"
  251.                (concat texinfo-tex-dvi-print-command
  252.                        " " texinfo-tex-root-temp-file ".dvi" "\n"))
  253.   (texinfo-recenter-tex-output-buffer nil))
  254.  
  255.  
  256. ;;; Texinfo TeX utility functions
  257.  
  258. (defun texinfo-tex-start-shell ()
  259.   (save-excursion
  260.     (require 'texinfo)
  261.     (set-buffer (make-shell "texinfo-tex-shell" "/bin/sh" nil "-v"))
  262.     (setq texinfo-tex-shell-map (copy-keymap shell-mode-map))
  263.     (texinfo-define-common-keys texinfo-tex-shell-map)
  264.     (use-local-map texinfo-tex-shell-map)
  265.     (run-hooks 'texinfo-tex-shell-hook)
  266.     (if (zerop (buffer-size))
  267.         (sleep-for 1))))
  268.  
  269. (defun texinfo-quit-tex-job ()
  270.   "Quit currently running TeX job, by sending an `x' to it."
  271.   (interactive)
  272.   (if (not (get-process "texinfo-tex-shell"))
  273.       (error "No TeX shell running."))
  274.   (save-excursion
  275.     (set-buffer (get-buffer "*texinfo-tex-shell*"))
  276.     (goto-char (point-max))
  277.     (insert "x")
  278.     (shell-send-input)))
  279.  
  280. (defun texinfo-kill-tex-job ()
  281.   "Kill the currently running TeX job."
  282.   (interactive)
  283.   (if (get-process "texinfo-tex-shell")
  284.         ;; Use `texinfo-tex-shell-sentinel' to restart
  285.         ;; texinfo-tex-shell after it is killed.
  286.         (kill-process (get-process "texinfo-tex-shell"))))
  287.  
  288. (defun texinfo-tex-shell-sentinel (process event)
  289.   "Restart texinfo-tex-shell after it is killed."
  290.   (if (equal event "killed\n")
  291.       (save-excursion
  292.         (set-buffer  "*texinfo-tex-shell*")
  293.         (insert "\n")
  294.         (texinfo-tex-start-shell))))
  295.  
  296. (defun texinfo-recenter-tex-output-buffer (linenum)
  297.   "Redisplay buffer of TeX job output so that most recent output can be seen.
  298. The last line of the buffer is displayed on
  299. line LINE of the window, or centered if LINE is nil."
  300.   (interactive "P")
  301.   (let ((texinfo-tex-shell (get-buffer "*texinfo-tex-shell*"))
  302.         (old-buffer (current-buffer)))
  303.     (if (null texinfo-tex-shell)
  304.         (message "No TeX output buffer")
  305.       (pop-to-buffer texinfo-tex-shell)
  306.       (bury-buffer texinfo-tex-shell)
  307.       (goto-char (point-max))
  308.       (recenter (if linenum
  309.                     (prefix-numeric-value linenum)
  310.                   (/ (window-height) 2)))
  311.       (pop-to-buffer old-buffer)
  312.       )))
  313.  
  314. (defun texinfo-show-tex-print-queue ()
  315.   "Show the print queue that \\[texinfo-tex-print] put your job on.
  316. Runs the shell command defined by texinfo-show-tex-queue-command."
  317.   (interactive)
  318.   (if (not (texinfo-tex-shell-running-p))
  319.       (texinfo-tex-start-shell))
  320.   (send-string "texinfo-tex-shell"
  321.                (concat texinfo-show-tex-queue-command "\n"))
  322.   (texinfo-recenter-tex-output-buffer nil))
  323.  
  324. (defun texinfo-delete-from-tex-print-queue (job-number)
  325.   "Delete job from the line printer spooling queue.
  326. You are prompted for the job number (shown by a previous
  327. \\[texinfo-show-tex-print-queue] command."
  328.   (interactive "nPrinter job number for deletion: ")
  329.   (if (texinfo-tex-shell-running-p)
  330.       (texinfo-kill-tex-job)
  331.     (texinfo-tex-start-shell))
  332.   (send-string "texinfo-tex-shell"
  333.                (concat 
  334.                 texinfo-delete-from-print-queue-command
  335.                 " "
  336.                 job-number"\n"))
  337.   (texinfo-recenter-tex-output-buffer nil))
  338.  
  339. (defun texinfo-tex-shell-running-p ()
  340.   (and (get-process "texinfo-tex-shell")
  341.        (eq (process-status (get-process "texinfo-tex-shell")) 'run)))
  342.  
  343.  
  344. ;;; Place `provide' at end of file.
  345. (provide 'texnfo-tex)
  346. ;;;;;;;;;;;;;;;; end texnfo-tex.el ;;;;;;;;;;;;;;;;
  347.